package com.dy.drp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dy.drp.dto.LoginDto;
import com.dy.drp.dto.UserDto;
import com.dy.drp.entity.Role;
import com.dy.drp.entity.User;
import com.dy.drp.entity.Userrole;
import com.dy.drp.mapper.RoleMapper;
import com.dy.drp.mapper.UserMapper;
import com.dy.drp.mapper.UserroleMapper;
import com.dy.drp.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.drp.util.PageUtil;
import com.dy.drp.util.SelectUtil;
import com.dy.drp.vo.UserAddVo;
import com.dy.drp.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dy
 * @since 2022-04-02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserroleMapper userroleMapper;

    @Override
    public UserVo getOneUserAndPowerByUser(User user) {
        List<LoginDto> userDtos = userMapper.getOneUserAndPowerByUser(user);
        UserVo userVo = new UserVo();
        userVo.setUserName(userDtos.get(0).getUserName());
        userVo.setRealName(userDtos.get(0).getRealName());
        List<Integer> power = new ArrayList<>();
        userDtos.stream().forEach(r->{
            power.add(r.getPowerId());
        });
        userVo.setUserPower(power);
        return userVo;
    }

    @Override
    public PageUtil<UserDto> selectOnePageUser(PageUtil<UserDto> pageUtil) {
        Integer integer = userMapper.selectUserCount();
        System.out.println(integer);
        pageUtil.setCount(integer);
        pageUtil.setTotalPage(integer);

        List<UserDto> userDtos = userMapper.selectOnePageUser(pageUtil);
        userDtos.stream().forEach(System.out::println);
        pageUtil.setList(userDtos);
        return pageUtil;
    }

    @Override
    public PageUtil<UserDto> selectOnePageUserByInfo(SelectUtil selectUtil) {
        PageUtil<UserDto> pageUtil = new PageUtil<>();
        pageUtil.setPageNum(selectUtil.getPageNum());
        pageUtil.setPageSize(selectUtil.getPageSize());

        Integer integer = userMapper.selectUserCountByInfo(selectUtil);
        pageUtil.setCount(integer);
        pageUtil.setTotalPage(integer);

        List<UserDto> userDtos = userMapper.selectOnePageUserByInfo(selectUtil);
        pageUtil.setList(userDtos);
        return pageUtil;
    }

    @Override
    @Transactional
    public boolean insertUser(UserDto userDto) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userDto.getUserName());
        User user = userMapper.selectOne(wrapper);
        if (user != null){
            return false;
        }
        userDto.setUserPwd("123456");
        int insert = userMapper.insertUser(userDto);
        Integer integer = userroleMapper.insertUserRole(userDto);
        if (integer > 0){
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean deleteUser(String userName) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName);
        User user = userMapper.selectOne(wrapper);

        QueryWrapper<Userrole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        userroleMapper.delete(queryWrapper);
        int delete = userMapper.delete(wrapper);
        if (delete > 0){
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public boolean updateUserRole(UserDto userDto) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userDto.getUserName());
        User user = userMapper.selectOne(wrapper);

        QueryWrapper<Userrole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getUserId());
        int delete = userroleMapper.delete(queryWrapper);
        if (delete <= 0){
            return false;
        }

        userDto.setUserId(user.getUserId());
        Integer integer = userroleMapper.insertUserRole(userDto);
        if (integer > 0){
            return true;
        }
        return false;
    }

    @Override
    public UserDto selectOneUserByName(String userName) {
        SelectUtil selectUtil = new SelectUtil();
        selectUtil.setName(userName);
        return userMapper.selectOneUserByName(selectUtil);
    }

}
